package com.scalar.chiptrack.finance.costCode.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.finance.creditNote.forms.CreditNoteForm;
import com.scalar.chiptrack.finance.creditNote.dao.CreditNoteDAO;
import com.scalar.chiptrack.finance.costCode.forms.CostCodeMasterForm;
import com.scalar.chiptrack.finance.costCode.dao.CostCodeMasterDAO;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.sales.SalesOrder;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewSalesOrderDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: KP
 * Date: Aug 03, 2007
 * Time: 7:22:26 PM
 */
public class CostCodeMasterSaveCommand extends ActionCommand
{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session = request.getSession();
        User user           = ( User ) session.getAttribute( SessionKeys.USER_KEY );
        HashMap errors      = ( HashMap ) request.getAttribute( SessionKeys.ERROR_MESSAGES );
        HashMap info        = ( HashMap ) request.getAttribute( SessionKeys.INFO_MESSAGES );

        CostCodeMasterForm costCodeForm = ( CostCodeMasterForm ) form;
        
        DBTransaction dbTransaction = null;
        boolean success             = false;
        
        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            CostCodeMasterDAO.saveCostCodeMaster( dbTransaction, costCodeForm.getWaferCodeCodeList() );
            CostCodeMasterDAO.saveCostCodeMaster( dbTransaction, costCodeForm.getPackageCodeCodeList() );
            CostCodeMasterDAO.saveCostCodeMaster( dbTransaction, costCodeForm.getPinCostCodeList() );

            //CostCodeMasterDAO.updateProductCostCodes( dbTransaction );

            success = true;
        }
        catch ( ScalarException e )
        {
            e.printStackTrace();
            errors.put ( "Could not save Cost Code Master", null );
        }
        finally
        {
            if ( dbTransaction != null )
            {
                if ( success )
                {
                    try
                    {
                        dbTransaction.commit();
                        info.put ( "Cost Code Master Information Saved Successfully", null );
                    }
                    catch( Exception e )
                    {
                        errors.put ( "Error while committing the database ", null );
                    }
                }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                        errors.put ( "Error while rollbacking the information", null );
                    }
                    catch( Exception e )
                    {
                        errors.put ( "Error while committing the database ", null );
                    }                   
                }
            }
        }

        dbTransaction   = null;
        success         = false;
        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            CostCodeMasterDAO.updateProductCostCodes( dbTransaction );

            success = true;
        }
        catch ( ScalarException e )
        {
            e.printStackTrace();
            errors.put ( "Could not assigned Cost Codes to Products", null );
        }
        finally
        {
            if ( dbTransaction != null )
            {
                if ( success )
                {
                    try
                    {
                        dbTransaction.commit();
                        info.put ( "Cost Codes successfully assigned to Products", null );
                    }
                    catch( Exception e )
                    {
                        errors.put ( "Could not assign Cost Codes to Products: Committing Problem", null );
                    }
                }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                        errors.put ( "Error while rollbacking the information", null );
                    }
                    catch( Exception e )
                    {
                        errors.put ( "Error while committing the database ", null );
                    }
                }
            }
        }

        try
        {
            costCodeForm.setWaferCodeCodeList   ( CostCodeMasterDAO.loadWaferPartNumber( null ) );
            costCodeForm.setPackageCodeCodeList ( CostCodeMasterDAO.loadPackageCostCodes( null ) );
            costCodeForm.setPinCostCodeList     ( CostCodeMasterDAO.loadPinCostCodes( null ) );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        return mapping.findForward ( "success" );
    }
}
